import 'package:flutter/material.dart';
import 'package:flutter_demo/pages/mine/mine_page.dart';


import '../book/book_page.dart';
import '../find/find_page.dart';
import '../home/home_page.dart';

class RootPage extends StatefulWidget {
  const RootPage({super.key});

  @override
  State<RootPage> createState() => _RootPageState();
}

class _RootPageState extends State<RootPage> {
  int _currentIndex = 0;

  final PageController _pageController = PageController();

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _buildBody,
      bottomNavigationBar: BottomNavigationBar(
          currentIndex: _currentIndex,
          selectedItemColor: Colors.black,
          unselectedItemColor: Colors.grey,
          showUnselectedLabels: true,
          iconSize: 18,
          selectedFontSize: 14,
          unselectedFontSize: 14,
          onTap: (pos) {
            setState(() {
              _currentIndex = pos;
              _pageController.jumpToPage(_currentIndex);
            });
          },
          items: items()),
    );
  }

  get _buildBody => PageView(
        controller: _pageController,
        onPageChanged: (pos) {
          setState(() {
            _currentIndex = pos;
          });
        },
        children: [
          HomePage(),
          BookPage(),
          FindPage(),
          MinePage(),
        ],
      );

  List<BottomNavigationBarItem> items() {
    return [
      BottomNavigationBarItem(
          icon: Image.asset("assets/icon/icon.png", width: 28, height: 28),
          label: "home"),
      BottomNavigationBarItem(
          icon: Image.asset("assets/icon/icon.png", width: 28, height: 28),
          label: "home"),
      BottomNavigationBarItem(
          icon: Image.asset("assets/icon/icon.png", width: 28, height: 28),
          label: "home"),
      BottomNavigationBarItem(
          icon: Image.asset("assets/icon/icon.png", width: 28, height: 28),
          label: "home"),
    ];
  }
}
